<template>
    <div>
        <div class="load" v-if="loading">loading...</div>
        <location-header :cities="cities"></location-header>
        <!-- 父子组件传值，通过属性绑定,hotCities和cities进行数据派发 -->
        <location-list :letter="letter" :cities="cities" :hotCities="hotCities" ></location-list>
        <!-- 接收LocationAlphabet传递过来的change事件 -->
        <location-alphabet @change="handleLetterChange" :alphabetList="alphabetList"></location-alphabet>
    </div>
</template>
 
<script>
// 引入axios
import axios from 'axios'

import locationHeader from './components/locationHeader'
import locationList from './components/locationList'
import locationAlphabet from './components/locationAlphabet'
export default {
    name: 'Location',
    components:{
        locationHeader,
        locationList,
        locationAlphabet
    },
    data(){
        return{
            letter: '',
            cities: [],
            alphabetList: [],
            hotCities: [],
            loading: true
        }
    },
    methods:{
        // 父组件做数据的中转
        handleLetterChange(letter){
            this.letter=letter
        },
        // city.json数据动态请求
        getLocationData(){
            axios.get("/api/city.json").then(this.getHomeDatasucc)
        },
        getHomeDatasucc(res){
            res =res.data
            if(res.data){
                this.loading=false
                const data=res.data
                this.cities=data.cities
                this.alphabetList=data.alphabetList
                this.hotCities=data.hotCities
            }
        }
    },
    //加载axios动态数据请求 
    mounted(){
        this.getLocationData()
    }
}
</script>

<style lang="stylus" scoped>
.load
    position: absolute
    top: 6rem
    left: 3.5rem
    z-index: 1011
    width: 2.8rem
    height: 1rem
    opacity: 0.5
    background: #bdbece
    color: #da1252
    text-align: center
    font-size: 25px
    line-height: 1rem
</style>
